package com.atmilan.quartz.job;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.SyncStatusEnum;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.service.ISystemConfigService;
import com.atmilan.quartz.sync.pull.europe.service.IEuropeSyncService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

/**
 * europe商品同步
 * @Description: 
 * @author daigb
 * @date 2021-03-23
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class EuropeSyncTask {
	@Autowired
    BaseTask baseTask;
    @Autowired
    IEuropeSyncService europeSyncService;
    @Autowired
    ISystemConfigService systemConfigService;

    /**
     * 每20分钟同步europe商品源
     * @Description:  
     * @author daigb 
     * @param 
     * @return void
     * @throws
     */
    @XxlJob("europeSyncTask")
    public void syncTask() {
        try {
            SystemConfig systemConfig = systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery()
                    .eq(SystemConfig::getName, Constants.EUROPE_SYNC_STATUS)
                    .last("LIMIT 1")
                    );
            SyncStatusEnum europeSyncStatus = SyncStatusEnum.valueOf(systemConfig.getFormId());
            long timeDiff = new Date().getTime() - systemConfig.getUpdateTime().getTime();

            log.warn("--------------- 开始计划同步europe商品, 同步状态为: {} ---------------", europeSyncStatus);
            // 24小时内状态还未变更, 直接重新处理
            if (SyncStatusEnum.WAIT == europeSyncStatus && 24 < (timeDiff / 1000 / 60 / 60)) {
                log.warn("--------------- europe商品同步过程超过24小时 ---------------");
                europeSyncStatus = SyncStatusEnum.PUSH;
            }
            // 有同步任务时不进行处理
            if (SyncStatusEnum.WAIT == europeSyncStatus) {
                log.warn("--------------- europe任务未结束, 新任务不执行 ---------------");
                return;
            }
            // 更新任务状态为同步中
            baseTask.updateSyscStatus(Constants.EUROPE_SYNC_STATUS, SyncStatusEnum.WAIT, systemConfig);
            // 下发完成, 需要同步
            if (SyncStatusEnum.PUSH == europeSyncStatus) {
                boolean result = europeSyncService.syncEuropeProduct();
                if (result) {
                	baseTask.updateSyscStatus(Constants.EUROPE_SYNC_STATUS, SyncStatusEnum.SYNC, systemConfig);
                    log.warn("--------------- europe商品同步完成!!! ---------------");
                }
                // 同步完成, 需要下发
            } else if (SyncStatusEnum.SYNC == europeSyncStatus) {
                boolean result = europeSyncService.pushEuropeProduct();
                if (result) {
                	baseTask.updateSyscStatus(Constants.EUROPE_SYNC_STATUS, SyncStatusEnum.PUSH, systemConfig);
                    log.warn("--------------- europe商品下发完成!!! ---------------");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
